local L = require('coevent')
print('start')

function test_http_client(id, host, uri)
	print('start test_http_client', id, host, uri)
	local cok = cosocket.tcp()
	cok:settimeout(3)
	local r,e = cok:connect(host, 80)
	--print(abc..'cc')

	if not r then print(1, e) return false end
	--print('----------------------------------------connected!!!', id)
	
	if not uri then
		cok:send('GET / HTTP/1.1\r\nHost: '..host..'\r\nUser-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)\r\n')
	else
		cok:send('GET '..uri..' HTTP/1.1\r\nHost: '..host..'\r\nUser-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)\r\n')
	end
	
	if not cok:send('Connection: close\r\n\r\n') then print('send error') return false end
	
	local s,e = cok:read('*l')

	cok:close()
	cok = nil
--print(oss)
	print('test_http_client ended', id, s, e)
	--if not e and not s then os.exit(1) end
	return id
end
--collectgarbage('stop')

local af = function()
	
	coroutine_wait(newthread(test_http_client, 0, 'wiki.upyun.com', '/index.php?title=%E9%A6%96%E9%A1%B5'))
	local t = longtime()
	
	t1 = newthread(test_http_client, 1, 'www.baidu.com')
	t2 = newthread(test_http_client, 2, 'weibo.com')
	t3 = newthread(test_http_client, 3, 'www.163.com')
	t4 = newthread(test_http_client, 4, 'www.twitter.com')
	t4 = newthread(test_http_client, 5, 'www.twdsffsdfsitter.com')	
	coroutine_wait(newthread(test_http_client, 1, 'wiki.upyun.com', '/index.php?title=%E9%A6%96%E9%A1%B5'))
	print("t4.id=", t3)
	
	print('times:', (longtime()-t)/1000)

	
	
	local t = longtime()
	local ts = {}
	for i=1,0 do --swop()
		print(i, coroutine_wait(newthread(test_http_client, i+100, 'wiki.upyun.com', '/index.php?title=%E9%A6%96%E9%A1%B5')))
	end
	print('times:', (longtime()-t)/1000)
	
	ts = nil
	
	
end

for u = 1,1 do
	L(af)
end

print('end')
